package com.itheima.Array.DoublePointer;

import java.util.Arrays;

/**
 * @Author: Ethan Yankang
 * @Program: code-random-recordings
 * @Date: 2024-07-28 19:01
 **/
public class SortedSquares {
    public int[] sortedSquares(int []  nums) {
        return newSorted1(nums);
    }

    private int[] newSorted1(int[] nums) {
        int left=0;
        int right= nums.length-1;
        int [] arr=new int[nums.length];
        int end=nums.length-1;
        while (left<=right){
            int L=nums[left]*nums[left];
            int R=nums[right]*nums[right];
            if (L>R){
                arr[end--]=L;
                left++;
            }else {
                arr[end--]=R;
                right--;
            }
        }
        return arr;
    }
    /*
 * @description 方法一：直接按绝对值排序
 * @params
 * @return
 */

    private int[] newSorted(int[] nums) {
       int len=nums.length;
       int i=0;
       /*
        *  剪枝操作
        */
        while (i<len&&nums[i]<0){
           nums[i]=-nums[i];
           i++;
        }
        Arrays.sort(nums);
        i=0;
        while (i<len){
            nums[i]*=nums[i];
        }
        return nums;
    }
}
